<template>
  <div class="app">
    <van-nav-bar
      title="购物车"
      left-text=""
      left-arrow
      @click="backlink()"
    ></van-nav-bar>
    <div class="cart_container" v-for="(shop, index) in shops" :key="index">
      <div class="cart_card">
        <van-checkbox
          v-model="shop.selectAll"
          @change="checkAllChange(shop)"
          class="shopname"
        >
          {{ shop.shopName }}<van-icon name="arrow" />
        </van-checkbox>
        <div
          class="goodsbox"
          v-for="(item, index1) in shop.items"
          :key="index1"
        >
          <!-- <van-swipe-cell class="goodsswipecell"> -->
          <van-checkbox
            @change="handleSelectItem(item)"
            v-model="item.checked"
            class="itemcheckbox"
          >
          </van-checkbox>
          <div class="goodscard">
            <div class="goodsimg">
              <img :src="item.image" alt="" />
            </div>
            <div class="goodsinfo">
              <div>
                <div
                  style="
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                  "
                >
                  <van-text-ellipsis
                    class="productName"
                    :content="item.productName"
                  />
                  <van-icon
                    name="delete-o"
                    class="deleteicon"
                    @click="deleteitem(item)"
                  />
                </div>

                <van-text-ellipsis
                  class="introdution"
                  :content="item.introdution"
                />
              </div>
              <div class="price-quantity">
                <div class="price">￥{{ item.price }}</div>
                <van-stepper
                  class="itemstepper"
                  @change="handlenumberchange(item)"
                  v-model="item.quantity"
                />
              </div>
            </div>
          </div>
          <!-- <template #right>
              <van-button
                square
                text="删除"
                type="danger"
                class="delete-button"
                @click="deleteitem(item)"
              />
            </template>
          </van-swipe-cell> -->
        </div>
      </div>
    </div>
    <div class="checkoutbox">
      <van-checkbox
        @change="handleSelectAll"
        v-model="selectAllcart"
        class="allcheckbox"
      >
        全选
      </van-checkbox>
      <div style="display: flex; align-items: center">
        <div style="font-size: 20px">合计：</div>
        <div style="font-size: 22px; color: #ff7141">￥ {{ totalPrice }}</div>
        <van-button class="checkbtn" type="primary">结算</van-button>
      </div>
    </div>
  </div>
</template>
  
<script>
import { showConfirmDialog } from "vant";
export default {
  data() {
    return {
      selectAllcart: false,
      totalPrice: 0,
      shops: [
        {
          shopName: "店铺1",
          selectAll: false,
          items: [
            {
              productName: "商品1xxxxxxxxxxxxxxxxxxxxxxxxxxx",
              image: require("../../assets/images/club1.jpg"),
              price: 10,
              introdution: "商品介绍",
              quantity: 2,
              checked: false,
            },
            {
              productName: "商品2",
              image: require("../../assets/images/club2.jpg"),
              price: 15,
              introdution: "xxxx",
              quantity: 1,
              checked: false,
            },
          ],
        },
        {
          shopName: "店铺2",
          selectAll: false,
          items: [
            {
              productName: "商品3",
              image: require("../../assets/images/club2.jpg"),
              price: 20,
              introdution: "xxxx",
              quantity: 1,
              checked: false,
            },
            {
              productName: "商品4",
              image: require("../../assets/images/club2.jpg"),
              price: 25,
              introdution: "xxxx",
              quantity: 3,
              checked: false,
            },
          ],
        },
      ],
      selectAll: false,
    };
  },
  methods: {
    backlink() {
      this.$router.back();
    },
    handlenumberchange(item) {
      this.updateTotalPrice();
    },
    handleSelectAll(value) {
      this.selectAllcart = value;
      this.shops.forEach((shop) => {
        shop.selectAll = value;
        shop.items.forEach((item) => {
          item.checked = value;
        });
      });
      this.updateTotalPrice();
    },
    deleteitem(item) {
      showConfirmDialog({
        message: "确认删除该商品吗？",
      })
        .then(() => {
          this.shops.forEach((shop, shopIndex) => {
            const index = shop.items.indexOf(item);
            if (index !== -1) {
              shop.items.splice(index, 1); // 从当前店铺的商品列表中删除商品
              if (shop.items.length === 0) {
                this.shops.splice(shopIndex, 1); // 如果当前店铺的商品列表为空，则删除整个店铺
              }
            }
          });
          this.updateTotalPrice(); // 更新总价
        })
        .catch(() => {
          // on cancel
        });
    },
    checkAllChange(shop) {
      shop.items.forEach((item) => {
        item.checked = shop.selectAll;
      });
      this.updateTotalPrice();
    },
    handleSelectItem(value) {
      const shopIndex = this.shops.findIndex((shop) =>
        shop.items.includes(value)
      );
      const shop = this.shops[shopIndex];
      const allChecked = shop.items.every((item) => item.checked);
      const allUnchecked = shop.items.every((item) => !item.checked);
      shop.selectAll = allChecked;
      this.selectAll = this.shops.every((shop) => shop.selectAll);
      this.updateTotalPrice();
    },
    updateTotalPrice() {
      let totalPrice = 0;
      this.shops.forEach((shop) => {
        shop.items.forEach((item) => {
          if (item.checked) {
            totalPrice += item.price * item.quantity;
          }
        });
      });
      this.totalPrice = totalPrice;
    },
    handleCheckout() {},
  },
};
</script>

<style scoped>
.app {
  margin: 0;
  padding: 0;
  background-color: #f2f3f5;
  min-height: 100vh;
}
.cart_container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.cart_card {
  width: 100%;
  margin: 10px auto 0 auto;
  border-radius: 15px;
  box-sizing: border-box;
  padding: 10px;
  background-color: white;
}
.shopname {
  font-size: 16px;
}
.goodsbox {
  display: flex;
  margin: 10px auto;
}
.goodsswipecell {
  width: 100%;
}
.itemcheckbox {
  width: 25px;
  margin-right: 5px;
}
.goodscard {
  width: 100%;
  height: 85px;
  display: flex;
  align-items: center;
}
.goodsimg {
  height: 80px;
  width: 80px;
  border-radius: 8px;
  overflow: hidden;
}
.goodsimg img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.goodsinfo {
  flex-grow: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  margin-left: 5px;
}
.productName {
  min-width: 50%;
  font-size: 18px;
}
.deleteicon {
  font-size: 18px;
  color: gray;
}
.introdution {
  font-size: 14px;
  color: gray;
}
.price-quantity {
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  height: 25px;
}
.price {
  font-size: 18px;
  color: #ff7141;
}
.checkoutbox {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 60px;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.allcheckbox {
  margin-left: 10px;
  font-size: 16px;
}
.checkbtn {
  width: 100px;
  height: 40px;
  margin-right: 10px;
  margin-left: 10px;
  border-radius: 25px;
}
.delete-button {
  height: 100%;
  margin-left: 5px;
}
</style>

<style>
.goodsswipecell .van-swipe-cell__wrapper {
  display: flex;
}
.itemstepper .van-stepper__minus,
.itemstepper .van-stepper__plus {
  width: 20px;
  height: 20px;
}

.itemstepper .van-stepper__input {
  width: 25px;
  height: 20px;
}
</style>
  